<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/ ' }">
        <i class="el-icon-s-promotion"/>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第三步：选择图片</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品相册" prop="albumId">
        <el-select v-model="ruleForm.albumId" placeholder="请选择">
          <el-option
              v-for="item in albumListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="back()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      albumListOptions: [
        {id: 1, name: '模拟相册数据1'},
        {id: 2, name: '模拟相册数据2'},
        {id: 3, name: '模拟相册数据3'}
      ],
      ruleForm: {
        albumId: '',
        albumName: ''
      },
      rules: {
        albumId: [
          {required: true, message: '请输入商品类别的ID', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    back() {
      this.$router.push('add-new-02');
    },
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('categoryItem');
      if (!localRuleFormString) {
        this.$router.push('add-new-01');
      }

      let localRuleForm = JSON.parse(localRuleFormString);
      console.log(localRuleForm);
      if (!localRuleForm.categoryId) {
        this.$router.push('add-new-01');
      } else if (!localRuleForm.brandId) {
        this.$router.push('add-new-02');
      }

      this.ruleForm = localRuleForm;
    },
    loadAlbumList() {
      let url = 'http://localhost:9080/albums/list';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.albumListOptions = responseBody.data;
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/albums/' + this.ruleForm.albumId;
          console.log('url = ' + url);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              let album = responseBody.data;
              this.ruleForm.albumId = album.id;
              this.ruleForm.albumName = album.name;
              // console.log(this.ruleForm);
              localStorage.setItem('categoryItem', JSON.stringify(this.ruleForm));
              this.$router.push('add-new-04');
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadAlbumList();
    this.loadLocalRuleForm();
  }
}
</script>

<style scoped>
</style>